<script>
import config from "./config";
import { getToken } from "@/utils/auth";
import appUpdateUtil from "@/utils/appUpdate";

export default {
  onLaunch: function () {
    console.log("App onLaunch - 应用启动");

    // #ifdef APP-PLUS
    console.log("App onLaunch - APP环境，延迟关闭启动页");
    // 延迟关闭启动页，确保登录检查完成后再关闭
    // #endif

    // #ifdef H5
    console.log("App onLaunch - H5环境，直接执行初始化");
    // #endif

    this.initApp();
    console.log("App onLaunch - 初始化完成");
  },
  onHide: function () {
    console.log("App onHide - 应用隐藏");
    // 应用隐藏时不清空token，只在真正关闭时清空
    // 这样可以避免用户切换应用到后台后需要重新登录
  },
  onUnload: function () {
    console.log("App onUnload - 应用卸载/关闭");
    // 应用真正关闭或卸载时清空token
    this.clearTokenOnClose();
  },
  methods: {
    // 初始化应用
    initApp() {
      console.log("App initApp - 开始初始化应用");
      try {
        // 初始化应用配置
        this.initConfig();
        console.log("App initApp - 配置初始化完成");

        // 移除启动时的版本检测，改为登录后进行
        // APP启动时不再进行版本检测，避免无token时的401错误
        // #ifdef APP-PLUS
        console.log("App initApp - 跳过启动时版本检测，将在登录后进行");
        // #endif

        // 检查用户登录状态
        this.checkLogin();
        console.log("App initApp - 登录检查完成");

        // 登录页跳转完成后关闭启动页
        // #ifdef APP-PLUS
        setTimeout(() => {
          try {
            plus.navigator.closeSplashscreen();
            console.log("App initApp - 登录页跳转完成后关闭启动页");
          } catch (e) {
            console.error("App initApp - 关闭启动页失败:", e);
          }
        }, 800);
        // #endif
      } catch (error) {
        console.error("App initApp - 初始化应用失败:", error);
        // 初始化失败也要确保能关闭启动页
        // #ifdef APP-PLUS
        setTimeout(() => {
          try {
            plus.navigator.closeSplashscreen();
            console.log("App initApp - 异常情况下关闭启动页");
          } catch (e) {
            console.error("App initApp - 异常关闭启动页失败:", e);
          }
        }, 1000);
        // #endif
      }
    },
    initConfig() {
      // 使用动态获取的baseUrl更新配置
      this.globalData.config = {
        ...config,
        baseUrl:
          config.baseUrl ||
          config.getDynamicBaseUrl?.() ||
          "http://172.168.1.11:48000",
      };
    },
    checkLogin() {
      console.log("App checkLogin - 直接跳转到登录页，不检查登录状态");
      try {
        // 直接跳转到登录页，不进行token检查
        console.log("App checkLogin - 直接跳转到登录页");
        this.$tab
          .reLaunch("/pages/login")
          .then(() => {
            console.log("App checkLogin - 跳转登录页成功");
          })
          .catch((err) => {
            console.error("App checkLogin - 跳转登录页失败:", err);
            // 容错处理，尝试其他跳转方式
            setTimeout(() => {
              uni.redirectTo({
                url: "/pages/login/login",
                success: () =>
                  console.log("App checkLogin - redirectTo登录页成功"),
                fail: (err2) =>
                  console.error(
                    "App checkLogin - redirectTo登录页失败:",
                    err2
                  ),
              });
            }, 500);
          });
      } catch (error) {
        console.error("App checkLogin - 跳转登录页异常:", error);
        // 异常时仍然跳转到登录页
        setTimeout(() => {
          this.$tab.reLaunch("/pages/login");
        }, 1000);
      }
    },
    // 应用关闭时清空token
    clearTokenOnClose() {
      console.log("App clearTokenOnClose - 清空token");
      try {
        // 导入removeToken函数
        const { removeToken } = require("@/utils/auth");
        removeToken();
        console.log("App clearTokenOnClose - token清空成功");
      } catch (error) {
        console.error("App clearTokenOnClose - 清空token失败:", error);
      }
    },
  },
};
</script>

<style lang="scss">
@import "@/static/scss/index.scss";

/* 现代化底部导航栏样式 */
.uni-tabbar {
  background: linear-gradient(180deg, #ffffff 0%, #f8f9fa 100%) !important;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08) !important;
  border-top: 1px solid rgba(0, 0, 0, 0.05) !important;
  backdrop-filter: blur(20px) !important;
  -webkit-backdrop-filter: blur(20px) !important;
}

/* 导航项样式优化 */
.uni-tabbar .uni-tabbar__item {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  position: relative !important;
  overflow: hidden !important;

  &:active {
    transform: scale(0.95) !important;
  }

  &::before {
    content: "" !important;
    position: absolute !important;
    top: 50% !important;
    left: 50% !important;
    width: 0 !important;
    height: 0 !important;
    background: radial-gradient(
      circle,
      rgba(46, 124, 246, 0.1) 0%,
      transparent 70%
    ) !important;
    border-radius: 50% !important;
    transform: translate(-50%, -50%) !important;
    transition: all 0.4s ease !important;
  }

  &:hover::before {
    width: 80px !important;
    height: 80px !important;
  }
}

/* 选中状态样式 */
.uni-tabbar .uni-tabbar__item--selected {
  background: linear-gradient(
    135deg,
    rgba(46, 124, 246, 0.1) 0%,
    rgba(46, 124, 246, 0.05) 100%
  ) !important;
  border-radius: 16px !important;
  margin: 4px !important;

  .uni-tabbar__icon {
    filter: drop-shadow(0 2px 4px rgba(46, 124, 246, 0.3)) !important;
    transform: translateY(-2px) !important;
  }

  .uni-tabbar__label {
    color: #2e7cf6 !important;
    font-weight: 600 !important;
    transform: translateY(-1px) !important;
  }
}

/* 图标样式优化 */
.uni-tabbar .uni-tabbar__icon {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1)) !important;

  &:hover {
    transform: translateY(-2px) !important;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.15)) !important;
  }
}

/* 标签文字样式 */
.uni-tabbar .uni-tabbar__label {
  transition: all 0.3s ease !important;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif !important;
  letter-spacing: 0.5px !important;
}

/* 移动端底部导航栏适配 */
@media screen and (max-width: 768px) {
  .uni-tabbar {
    height: 100rpx !important;
    padding-bottom: constant(safe-area-inset-bottom) !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
  }

  .uni-tabbar .uni-tabbar__item {
    height: 100rpx !important;
  }

  .uni-tabbar .uni-tabbar__icon {
    width: 48rpx !important;
    height: 48rpx !important;
    margin-bottom: 6rpx !important;
  }

  .uni-tabbar .uni-tabbar__icon.uni-tabbar__icon__img {
    width: 48rpx !important;
    height: 48rpx !important;
  }

  .uni-tabbar .uni-tabbar__label {
    font-size: 24rpx !important;
    line-height: 1.4 !important;
  }
}

/* 小屏幕设备优化 */
@media screen and (max-width: 375px) {
  .uni-tabbar .uni-tabbar__icon {
    width: 44rpx !important;
    height: 44rpx !important;
  }

  .uni-tabbar .uni-tabbar__label {
    font-size: 22rpx !important;
  }
}

/* 横屏模式适配 */
@media screen and (orientation: landscape) and (max-width: 896px) {
  .uni-tabbar {
    height: 90rpx !important;
  }

  .uni-tabbar .uni-tabbar__item {
    height: 90rpx !important;
  }

  .uni-tabbar .uni-tabbar__icon {
    width: 40rpx !important;
    height: 40rpx !important;
  }

  .uni-tabbar .uni-tabbar__label {
    font-size: 20rpx !important;
  }
}

/* 动画效果 */
@keyframes tabbar-bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-4px);
  }
}

@keyframes tabbar-glow {
  0%,
  100% {
    filter: drop-shadow(0 2px 4px rgba(46, 124, 246, 0.3));
  }
  50% {
    filter: drop-shadow(0 4px 8px rgba(46, 124, 246, 0.5));
  }
}

@keyframes tabbar-pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.05);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.uni-tabbar .uni-tabbar__item--selected .uni-tabbar__icon {
  animation: tabbar-bounce 0.6s ease-in-out !important;
}

.uni-tabbar .uni-tabbar__item--selected {
  animation: tabbar-pulse 2s infinite ease-in-out !important;
}

.uni-tabbar .uni-tabbar__item--selected .uni-tabbar__label {
  animation: tabbar-glow 1.5s infinite ease-in-out !important;
}

/* 暗黑模式适配 */
@media (prefers-color-scheme: dark) {
  .uni-tabbar {
    background: linear-gradient(180deg, #1a1a1a 0%, #2d2d2d 100%) !important;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.3) !important;
    border-top: 1px solid rgba(255, 255, 255, 0.05) !important;
  }

  .uni-tabbar .uni-tabbar__item--selected {
    background: linear-gradient(
      135deg,
      rgba(46, 124, 246, 0.2) 0%,
      rgba(46, 124, 246, 0.1) 100%
    ) !important;
  }

  .uni-tabbar .uni-tabbar__label {
    color: #e0e0e0 !important;
  }

  .uni-tabbar
    .uni-tabbar__item:not(.uni-tabbar__item--selected)
    .uni-tabbar__label {
    color: #999999 !important;
  }
}
</style>
